package app.msy.cn.takephoto.util;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import app.msy.cn.takephoto.FilterImageActivity;
import jp.co.cyberagent.android.gpuimage.GPUImage;
import jp.co.cyberagent.android.gpuimage.GPUImageBrightnessFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageContrastFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageDivideBlendFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageExposureFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageFilterGroup;
import jp.co.cyberagent.android.gpuimage.GPUImageGaussianBlurFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageOpacityFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageSaturationFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageSharpenFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageToneCurveFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageTwoInputFilter;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

public class ImageUtil {

    private static final String TAG = "ImageUtil";
    private static List<File> images;
    private static List<String> imagePaths;
    private static List<String> compressedImagePaths;

    private static OnImagesCompressedListener compressListener;
    private static OnSingleImageCompressedListener singleCompressedListener;

    /**
     * 功能描述：获取图片旋转度数
     */
    public static int getImageDegree(String path) {
        File file = new File(path);
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(file.getPath());
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 90;
    }

    /**
     * 功能描述：旋转图片
     */
    public static String rotateImageByDegree(String path, int degree) {
        Bitmap bitmap = BitmapFactory.decodeFile(path);

        Matrix matrix = new Matrix();
        matrix.postRotate(degree);

        Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

        String filePath = FileUtil.getImageDir() + System.currentTimeMillis() + ".jpg";
        String newFilePath = FileUtil.saveBitmap(newBitmap, filePath);

        bitmap.recycle();
        newBitmap.recycle();

        return newFilePath;
    }

    /**
     * 压缩多张图片回调
     */
    public interface OnImagesCompressedListener {
        void onSuccess(List<File> compressedFiles, List<String> compressedFilesPath);
    }

    /**
     * 压缩1张图片回调
     */
    public interface OnSingleImageCompressedListener {
        void onSuccess(File file);
    }

    /**
     * 功能描述：压缩多个文件
     */
    public static void compressImages(Context context, List<String> pathList, OnImagesCompressedListener listener) {
        if (pathList != null) {
            compressListener = listener;
            images = new ArrayList<>();
            compressedImagePaths = new ArrayList<>();
            imagePaths = pathList;
            for (String path : pathList) {
                compress(context, path);
            }
        }
    }

    /**
     * 功能描述：压缩1个文件
     */
    public static void compressImage(Context context, String path, OnSingleImageCompressedListener listener) {
        if (path != null) {
            singleCompressedListener = listener;
            compress(context, path);
        }
    }

    /**
     * 功能描述：压缩
     */
    private static void compress(Context context, String filePath) {
        File file = new File(filePath);
        Log.i(TAG, "compress: fileSize" + file.length());
        Luban.get(context)
                .load(file)                     //传人要压缩的图片
                .putGear(Luban.THIRD_GEAR)      //设定压缩档次，默认三挡
                .setCompressListener(new MyOnCompressListener()).launch();    //启动压缩
    }

    private static class MyOnCompressListener implements OnCompressListener {

        @Override
        public void onStart() {
            Log.i(TAG, "onStart: " + System.currentTimeMillis());
        }

        @Override
        public void onSuccess(File file) {
            Log.i(TAG, "onSuccess: fileSize" + file.length());
            if (images != null) {
                images.add(file);
            }
            if (singleCompressedListener != null) {//压缩一个文件的回调
                singleCompressedListener.onSuccess(file);
            }
            if (compressedImagePaths != null) {
                compressedImagePaths.add(file.getPath());
            }
            if (imagePaths != null && imagePaths.size() == images.size()) {//压缩多个文件的回调
                compressListener.onSuccess(images, compressedImagePaths);
                Log.i(TAG, "onSuccess: filePath" + compressedImagePaths.toString());
            }
        }

        @Override
        public void onError(Throwable e) {
            Log.i(TAG, "onError: " + e.getMessage());
        }
    }

    /**
     * 功能描述：对图片进行滤镜处理,图片->扫描文件
     */
    public static Bitmap filterImage(String path, Context context) {
        Bitmap bitmap = BitmapFactory.decodeFile(path);

        GPUImage gpuImage1 = new GPUImage(context);
        gpuImage1.setImage(bitmap);
        gpuImage1.setFilter(new GPUImageGaussianBlurFilter(10f));
        Bitmap bitmap1 = gpuImage1.getBitmapWithFilterApplied();

//        GPUImage gpuImage2 = new GPUImage(context);
//        gpuImage2.setImage(bitmap);
//        gpuImage2.setFilter(new GPUImageContrastFilter(100f));
//        Bitmap bitmap2 = gpuImage2.getBitmapWithFilterApplied();

        GPUImageDivideBlendFilter gpuImageMultiplyBlendFilter = new GPUImageDivideBlendFilter();
        gpuImageMultiplyBlendFilter.setBitmap(bitmap1);

        GPUImageFilterGroup group = new GPUImageFilterGroup();
        group.addFilter(gpuImageMultiplyBlendFilter);
//        group.addFilter(new GPUImageContrastFilter(50f));//对比度
        group.addFilter(new GPUImageSaturationFilter(1f));//饱和度
//        group.addFilter(new GPUImageSharpenFilter(2f));//锐化 GPUImageSharpenFilter
//        group.addFilter(new GPUImageExposureFilter(0f));//曝光
        group.addFilter(new GPUImageBrightnessFilter(0.1f));//亮度
//        group.addFilter(new GPUImageOpacityFilter(0.7f));//提亮阴影

        GPUImage gpuImage = new GPUImage(context);
        gpuImage.setImage(bitmap);
        gpuImage.setFilter(group);
        Bitmap bitmap3 = gpuImage.getBitmapWithFilterApplied();

        return bitmap3;
    }
}
